//
//  GameCollectionViewCell.m
//  UI_Project_A
//
//  Created by GH on 16/2/28.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "GameCollectionViewCell.h"
#import "GameTableViewCellOfOne.h"
#import "HandleData.h"
#import "ModelOfGame.h"
#import "CellTwoTab.h"
#import "CellTwoTab2.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <DKNightVersion.h>


#define  WIDTH self.frame.size.width

#define HEIGHT self.frame.size.height



@interface GameCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,retain)NSMutableArray *picarr;
@property (nonatomic,assign)NSInteger number;



@end


@implementation GameCollectionViewCell




- (void) dealloc {

    
    [_picarr release];
    [_arr release];
    [_tableview release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame {


    self = [super initWithFrame:frame];
    
    if (self) {
        [self data];
        [self createTableview];
        [self refresh];
    }


    return self;

}


- (void)data {
    self.arr = [NSMutableArray array];
    self.number = 2;
     NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10178&cattype=news&page=%ld&i_=868299012353735&t_=1453899132647&p_=10286&v_=400609&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=15&cha=tencentMartket&u_=&",self.number++];
    
    [HandleData handleFloewNewWithString:str Block:^(id result) {
        self.picarr = [NSMutableArray array];
        
        NSMutableArray *arrshuju = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in arrshuju) {
            
            ModelOfGame *model = [[ModelOfGame alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            NSString *str  =   [dic objectForKey:@"id"];
            model.idd = str;
            [self.arr addObject:model];
            
            [model release];
            [self.tableview reloadData];

        }

        
        
    }];
    


}

#pragma 上拉刷新
- (void)refresh {
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        NSLog(@"上拉刷新");
        
        NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10178&cattype=news&page=%ld&i_=868299012353735&t_=1453899132647&p_=10286&v_=400609&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=15&cha=tencentMartket&u_=&",self.number++];
        
        [HandleData handleFloewNewWithString:str Block:^(id result) {
            
            
            self.picarr = [NSMutableArray array];
            
            
            NSMutableArray *arrshuju = [result objectForKey:@"data"];
            
            
            for (NSDictionary *dic in arrshuju) {
                
                ModelOfGame *model = [[ModelOfGame alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                NSString *str  =   [dic objectForKey:@"id"];
                model.idd = str;
                [self.arr addObject:model];
                
                
                
                [model release];
            }
            
            
            
            [self.tableview.mj_footer endRefreshing];
            [self.tableview reloadData];
            
        }];
        

}];

}



- (void)createTableview {

    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    
    [self.contentView addSubview:self.tableview];
    [self.tableview release];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    [self.tableview registerClass:[GameTableViewCellOfOne class] forCellReuseIdentifier:@"pool"];
    [self.tableview registerClass:[CellTwoTab class] forCellReuseIdentifier:@"pool1"];
    [self.tableview registerClass:[CellTwoTab2 class] forCellReuseIdentifier:@"pool2"];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arr.count ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//    if (indexPath.row == 0) {
//        GameTableViewCellOfOne *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
//        
//        return cell;
//    }
//
    ModelOfGame *model = [self.arr objectAtIndex:indexPath.row];
    if ([model.pic_url isEqualToString:@""]) {
        CellTwoTab *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
        
        
        cell.title.text = model.title;
        cell.desc.text = model.desc;
        
        cell.title.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
        cell.desc.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
        cell.time.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
        
        
        
        NSString *str = model.published;
        
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
        cell.time.text =  currentDateStr;
        
        if (![model.weight_new isEqualToString:@"0"]) {
            
            cell.image.hidden = NO;
        }
        
        return cell;
    }else {
    
        CellTwoTab2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
        cell.title.text = model.title;
        cell.desc.text = model.desc;
        cell.title.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
        cell.desc.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
        cell.time.dk_textColorPicker =DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
        
        
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        
        NSString *str = model.published;
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//        NSLog(@"date:%@",[detaildate description]);
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
//        NSLog(@"%@",currentDateStr);
        cell.time.text =  currentDateStr;
        
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
              return cell;
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return WIDTH * 0.4;
    }else{
        return WIDTH * 0.225 + 30;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
 [self.delegate didClickWith:[self.arr objectAtIndex:indexPath.row]];
    
    
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    self.tableview.frame = self.contentView.frame;
    
}

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    return  layoutAttributes;
    
}



@end
